package IBBB.view;

import IBBB.model.UserMessage;
import IBBB.model.WordStockMessage;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.ObservableMap;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Insets;
import javafx.geometry.Orientation;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.HBox;

import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class NewPlanStock extends FlowPane {
    private UserMessage userMessage;
    private ObservableMap<String, Integer> wordStock;
    private ObservableMap<StringProperty, List<IntegerProperty>> userSchedule;
    private WordStockMessage wordStockMessage;
    public List<NewPlanBox> getScheduleBoxes() {
        return newPlanBoxes;
    }
    private List<NewPlanBox> newPlanBoxes = new LinkedList<>();

    public NewPlanStock(UserMessage message, WordStockMessage wordStockMessage) throws IOException {
        this.wordStockMessage = wordStockMessage;
        this.userMessage = message;
        this.userSchedule = message.getWordStockInfo();
        this.wordStock=wordStockMessage.getMessage();
        this.setVgap(10.0);
        initialize();
    }

    public void initialize() throws IOException {
        this.getChildren().clear();
        this.newPlanBoxes.clear();
        for (Map.Entry<String, Integer> item: wordStock.entrySet()) {
            FXMLLoader loader = new FXMLLoader();
            loader.setLocation(getClass().getResource("NewPlanBox.fxml"));
            HBox box = loader.load();
            NewPlanBox boxController = loader.getController();
            newPlanBoxes.add(boxController);
            boolean planned=false;
            for(Map.Entry<StringProperty, List<IntegerProperty>> sch: userSchedule.entrySet()){
                String wordStockString=item.getKey();
                String userPlanString=sch.getKey().getValue();
                if(wordStockString.equals(userPlanString))
                    planned=true;
            }
            boxController.setMessage(userMessage,item.getKey(), item.getValue(),planned);
            this.getChildren().add(box);
        }
    }
}
